//
//  HomeCollectionViewCell.m
//  SSWiFi
//
//  Created by 酷安 @啥也不会啊啊啊啊啊啊  on 2024/1/23.
//

#import "HomeCollectionViewCell.h"
#import "HomeItemTopView.h"

@interface HomeCollectionViewCell()
@property (weak, nonatomic) IBOutlet UILabel *lb_tiltle1;
@property (weak, nonatomic) IBOutlet UILabel *lb_value1;
@property (weak, nonatomic) IBOutlet UILabel *lb_title2;
@property (weak, nonatomic) IBOutlet UILabel *lb_value2;

@property (weak, nonatomic) IBOutlet UILabel *lb_subTitle;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (strong,nonatomic) HomeItemTopView *topview;

@property (weak, nonatomic) IBOutlet UIImageView *img_value1Edit;
@property (weak, nonatomic) IBOutlet UIImageView *img_value2Edit;


@end

@implementation HomeCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.bgView.backgroundColor = [UIColor qd_backgroundColorLighten];
    self.bgView.layer.cornerRadius = 5;
    [self.bgView clipsToBounds];
    self.lb_tiltle1.textColor = [UIColor qd_titleTextColor];
    self.lb_value1.textColor = [UIColor qd_titleTextColor];
    self.lb_title2.textColor = [UIColor qd_titleTextColor];
    self.lb_value2.textColor = [UIColor qd_titleTextColor];
    self.lb_subTitle.textColor =[UIColor qd_descriptionTextColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lb_value1_tap) ];
    [self.lb_value1 addGestureRecognizer:tap];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lb_value2_tap) ];
    [self.lb_value2 addGestureRecognizer:tap2];
    
    UITapGestureRecognizer *edittap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(img_edit_value1_tap) ];
    UITapGestureRecognizer *edittap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(img_edit_value2_tap) ];
    [self.img_value1Edit addGestureRecognizer:edittap1];
    [self.img_value2Edit addGestureRecognizer:edittap2];
}

- (void)lb_value1_tap{
    if([self.celldelegate respondsToSelector:@selector(value1Click:)]){
        if(self.model.type == HomeCellType_IMEI){
            [self.celldelegate value1Click:self.model.model.imei];
        }
    }
}

- (void)lb_value2_tap{
    if([self.celldelegate respondsToSelector:@selector(value1Click:)]){
        if(self.model.type == HomeCellType_IMEI){
            [self.celldelegate value1Click:self.model.model.ziccid];
        }else if(self.model.type == HomeCellType_WiFiInfo){
            [self.celldelegate value1Click:[self.model.model.WPAPSK1_encode base64String]];
        }
    }
}

- (void)img_edit_value1_tap{
    if([self.celldelegate respondsToSelector:@selector(edit1Click:)]){
        [self.celldelegate edit1Click:self.model];
    }
}

- (void)img_edit_value2_tap{
    if([self.celldelegate respondsToSelector:@selector(edit2Click:)]){
        [self.celldelegate edit2Click:self.model];
    }
}


- (void)setModel:(HomeItemModel *)model{
    _model = model;
    self.lb_subTitle.text = @"";
    self.lb_tiltle1.text = @"";
    self.lb_value1.text = @"";
    self.lb_title2.text = @"";
    self.lb_value2.text = @"";
    self.lb_value1.userInteractionEnabled = NO;
    self.lb_value2.userInteractionEnabled = NO;
    [self.img_value1Edit setHidden:YES];
    [self.img_value2Edit setHidden:YES];
//    self.img_value1Edit.userInteractionEnabled = NO;
//    self.img_value2Edit.userInteractionEnabled = NO;
    [self.topview setHidden:YES];
    if(model.type == HomeCellType_Network){
        if(model.model.network_provider.length == 0){
            model.model.network_provider = @"-";
        }
        self.lb_tiltle1.text = model.model.network_provider;
        self.lb_value1.text = model.model.sub_network_type;
        if(model.model.sub_network_type.length == 0){
            model.model.sub_network_type = @"-";
        }
        self.lb_title2.text = @"信号强度";
        self.lb_value2.text = [NSString stringWithFormat:@"%@ dBm",model.model.lte_rsrp];
    }else if (model.type == HomeCellType_NetConnTime){
        [self.topview setHidden:NO];
        [self.topview setModel:model.model];
        self.lb_tiltle1.text = @"当前频段";
        NSString *band =[NSString stringWithFormat:@"B%@",model.model.lte_band];
        if(model.model.lte_band.length == 0){
            band = @"-";
        }
        self.lb_value1.text = band;
        self.lb_title2.text = @"网络连接";
        self.lb_value2.text = [model.model.realtime_time toHMS];
    }else if (model.type == HomeCellType_IMEI){
        self.lb_tiltle1.text = @"IMEI";
        self.lb_value1.text = model.model.imei;
        self.lb_title2.text = @"ICCID";
        self.lb_value2.text = model.model.ziccid;
        self.lb_value1.userInteractionEnabled = YES;
        self.lb_value2.userInteractionEnabled = YES;
        [self.img_value1Edit setHidden:NO];
    }else if (model.type == HomeCellType_CurrentUseByte){
        self.lb_tiltle1.text = @"本次使用流量";
        NSInteger current_use = (model.model.realtime_tx_bytes.integerValue + self.model.model.realtime_rx_bytes.integerValue) ;
        
        self.lb_value1.text = [NSString bytesToGMK:current_use];
        
        self.lb_title2.text = @"已连接设备";
        self.lb_value2.text = [NSString stringWithFormat:@"%@ 台",self.model.model.sta_count];
    }
    else if (model.type == HomeCellType_RealtimeUseByte){
        self.lb_subTitle.text = self.model.title;
        self.lb_tiltle1.text = @"上传 ↑";
        self.lb_value1.text = [NSString stringWithFormat:@"%@/s",[model.model.realtime_tx_thrpt bytesToGMK]];
        
        self.lb_title2.text = @"下载 ↓";
        self.lb_value2.text =  [NSString stringWithFormat:@"%@/s",[model.model.realtime_rx_thrpt bytesToGMK]];
        self.lb_subTitle.text = self.model.title;
    }
    else if (model.type == HomeCellType_TotalUseByte){
        self.lb_subTitle.text = self.model.title;
        self.lb_tiltle1.text = @"已用流量";
        NSInteger month_total = model.model.monthly_rx_bytes.integerValue + model.model.monthly_tx_bytes.integerValue;
        self.lb_value1.text = [NSString bytesToGMK:month_total];
        
        self.lb_title2.text = @"套餐总量";
        
        NSInteger total = 0;
        NSArray *arr = [model.model.data_volume_limit_size componentsSeparatedByString:@"_"];
        if(arr.count ==2){
            total = [arr[0] floatValue] * [arr[1] integerValue] * 1024 * 1024;
        }
        NSString *value2 = @"未设置";
        if(total > 0){
            value2 = [NSString bytesToGMK:total];
        }
        self.lb_value2.text = value2;
        self.lb_subTitle.text = self.model.title;
        [self.img_value2Edit setHidden:NO];
    }
    else if (model.type == HomeCellType_WiFiInfo){
        
        self.lb_subTitle.text = self.model.title;
        self.lb_tiltle1.text = @"WiFi名称";
        self.lb_value1.text =  self.model.model.SSID1;
        
        self.lb_title2.text = @"WiFi密码";
        self.lb_value2.text = [self.model.model get_ssid_pwd];
        self.lb_subTitle.text = self.model.title;
        
        [self.img_value1Edit setHidden:NO];
//        [self.img_value2Edit setHidden:NO];
//        self.lb_value1.userInteractionEnabled = NO;
        self.lb_value2.userInteractionEnabled = YES;
    }
    else if (model.type == HomeCellType_WiFiSeting){
        self.lb_subTitle.text = self.model.title;
        self.lb_tiltle1.text = @"WiFi范围";
        self.lb_value1.text =  [self.model.model get_wifi_coveragestr];
        [self.img_value1Edit setHidden:NO];
    }
    
}


- (HomeItemTopView *)topview{
    if(!_topview){
        _topview = [HomeItemTopView defautView];
//        _topview.backgroundColor = UIColor.redColor;
        [self.bgView insertSubview:_topview aboveSubview:self.lb_subTitle];
        [_topview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgView).offset(10);
            make.right.equalTo(self.bgView).offset(-10);
            make.height.equalTo(@15);
            make.width.equalTo(@60);
        }];
        _topview.backgroundColor = [UIColor qd_backgroundColorLighten];
    }
    return _topview;
}
@end
